package com.appspot.showup_app.showup;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.appspot.showup_app.showup.model.ShowUpCustomerMessage;
import com.appspot.showup_app.showup.tasks.UpdateCustomerTask;

/**
 * Created by Gal on 12/4/2014.
 */
public class EditCustomerActivity extends ActionBarActivity {
    private static final String LOG_TAG = "EditCustomerActivity";
    private static final String TAG = "Showup";


    ShowUpCustomerMessage customer;
    EditText editName;
    EditText editPhoneNumber;
    EditText editEmail;
    EditText editAddress;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_customer);

        if (!ApplicationData.getInstance().isLoggedIn())
        {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }

        editName = (EditText) findViewById(R.id.editName);
        editPhoneNumber = (EditText) findViewById(R.id.editPhoneNumber);
        editEmail = (EditText) findViewById(R.id.editEmail);
        editAddress = (EditText) findViewById(R.id.editAddress);


        String customerId = getIntent().getStringExtra("CUSTOMER_ID");

        DatabaseHelper mDbHelper = new DatabaseHelper(this);
        db = mDbHelper.getWritableDatabase();

        if (customerId == null)
            customer = new ShowUpCustomerMessage();
        else
            customer = DatabaseHelper.getCustomer(db, customerId);

        editName.setText(customer.getName());
        editPhoneNumber.setText(customer.getPhoneNumber());
        editEmail.setText(customer.getEmail());
        editAddress.setText(customer.getAddress());

    }

    public void onClickEditCustomer(View view) {

        customer.setAddress(editAddress.getText().toString());
        customer.setEmail(editEmail.getText().toString());
        customer.setName(editName.getText().toString());
        customer.setPhoneNumber(editPhoneNumber.getText().toString());
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Updating client...");
        progressDialog.show();

        UpdateCustomerTask updateCustomerTask = new UpdateCustomerTask(this, customer) {
            @Override
            protected void onPostExecute(ShowUpCustomerMessage editedCustomer) {
                if (editedCustomer != null) {
                    DatabaseHelper.addEditCustomer(db, editedCustomer);
                    Toast.makeText(EditCustomerActivity.this, "Customer updated!", Toast.LENGTH_LONG);
                    setResult(RESULT_OK, new Intent());
                    progressDialog.dismiss();
                    finish();
                }
                else {
                    Toast.makeText(EditCustomerActivity.this, "Error updating", Toast.LENGTH_LONG);
                    finish();
                }
            }
        };
        updateCustomerTask.execute((Void)null);

    }
}
